package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.type.GoogleMapsType;
import com.aplink.generic.pojo.controlspec.GoogleMapsSpec;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class GoogleMapsFormat {
	public static final String CAMERA_BEARING = "cameraBearing";
	public static final String CAMERA_TARGET_LAT = "cameraTargetLat";
	public static final String CAMERA_TARGET_LNG = "cameraTargetLng";
	public static final String CAMERA_TILT = "cameraTilt";
	public static final String CAMERA_ZOOM = "cameraZoom";
	private static final String CONTENT_DATA = "contentdata";
	private static final String FIELD = "field";
	private static final String INFO_WINDOWS_DESIGN = "rowitem";
	public static final String MAP_TYPE = "mapType";
	private static final String NAME = "name";
	private static final String RPTR_DESIGN = "rptrdesign";
	public static final String SET_GPS_LOCATION = "isSetGPSLocation";
	public static final String UI_COMPASS = "uiCompass";
	public static final String UI_ROTATE_GESTURES = "uiRotateGestures";
	public static final String UI_SCROLL_GESTURES = "uiScrollGestures";
	public static final String UI_ZOOM_CONTROLS = "uiZoomControls";
	public static final String UI_ZOOM_GESTURES = "uiZoomGestures";

	public static GoogleMapsSpec parse(final Node node) {
		final GoogleMapsSpec googleMapsSpec = new GoogleMapsSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.MAP_TYPE);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setGoogleMapsType(GoogleMapsType.get(value));
			}

			value = XmlPaserUtil
			        .getAttrValue(node, GoogleMapsFormat.UI_COMPASS);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("N")) {
					googleMapsSpec.setUiCompass(false);
				}
			} else {
				googleMapsSpec.setUiCompass(true);
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.UI_ROTATE_GESTURES);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("N")) {
					googleMapsSpec.setUiRotateGestures(false);
				}
			} else {
				googleMapsSpec.setUiRotateGestures(true);
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.UI_SCROLL_GESTURES);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("N")) {
					googleMapsSpec.setUiScrollGestures(false);
				}
			} else {
				googleMapsSpec.setUiScrollGestures(true);
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.UI_ZOOM_GESTURES);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("N")) {
					googleMapsSpec.setUiZoomGestures(false);
				}
			} else {
				googleMapsSpec.setUiZoomGestures(true);
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.UI_ZOOM_CONTROLS);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("Y")) {
					googleMapsSpec.setUiZoomControls(true);
				}
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.CAMERA_ZOOM);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setCameraZoom(Double.parseDouble(value));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.CAMERA_TILT);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setCameraTilt(Double.parseDouble(value));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.CAMERA_BEARING);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setCameraBearing(Double.parseDouble(value));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.CAMERA_TARGET_LAT);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setCameraTargetLat(Double.parseDouble(value));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.CAMERA_TARGET_LNG);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setCameraTargetLng(Double.parseDouble(value));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        GoogleMapsFormat.SET_GPS_LOCATION);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("Y")) {
					googleMapsSpec.setLoadGPSLocation(true);
				}
			}

			value = XmlPaserUtil.getAttrValue(node, CONTENT_DATA);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setContentData(value);
			}

			value = XmlPaserUtil.getAttrValue(node, INFO_WINDOWS_DESIGN);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setInfoWindowsDesign(value);
			}

			value = XmlPaserUtil.getAttrValue(node, RPTR_DESIGN);
			if (!TextUtils.isEmpty(value)) {
				googleMapsSpec.setColumnSetting(value);
			}
		}
		final NodeList nodeList = ((Element) node).getElementsByTagName(FIELD);
		Node field = null;
		String value = null;
		for (int i = 0; i < nodeList.getLength(); i++) {
			field = nodeList.item(i);
			if (field.hasAttributes()) {
				value = XmlPaserUtil.getAttrValue(field, NAME);
				if (!TextUtils.isEmpty(value)) {
					googleMapsSpec.getSearchFields().add(value);
				}
			}
		}
		return googleMapsSpec;
	}
}
